<template>
  <el-dialog
    v-el-drag-dialog="{ drag: true, full: false, resize: false }"
    :visible="true"
    :close-on-click-modal="false"
    @close="closeMd"
    :append-to-body="true"
    width="350px"
    custom-class="create-team-order"
    title="生成车队订单">
    <el-form
      :model="fm.ps"
      :ref="fm.fname"
      :label-width="'100px'"
      @keyup.enter.native="onSubmit">
      <div class="content">
        <b>您确定将所选<span class="c-red">{{ myData.ids && myData.ids.split(',').length }}条</span>导入订单生成车队订单吗？</b>
      </div>

      <el-form-item>
        <el-button 
          type="info" 
          icon="el-icon-close"
          @click="closeMd">
          关 闭
        </el-button>

        <el-button 
          type="primary" 
          icon="el-icon-check"
          @click="onSubmit" 
          :loading="fm.sb.load">
          {{ fm.sb.text }}
        </el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import elDragDialog from "@/directive/el-drag-dialog";

import * as orderSer from '@/api/services/order.service';
export default {
  name: "CreateTeamOrder",
  directives: { elDragDialog },
  props: {myData: Object},
  data() {
    return {
      // 是否是手动生成订单
      isSelfCreate: this.myData.isSelfCreate || false,
      fm: {
        fname: "createTeamOrderFm",
        sb: { load: false, text: "立即生成" },
        ps: {
          // 生成的订单id数组字符串
          ids: this.myData.ids || '',
        },
      },
      
    };
  },
  created() {
    const that = this;
    console.log('this.myData', this.myData);
  },
  methods: {
    /* 关闭弹框 */
    closeMd() {
      this.$emit("myClick", {state: false});
    },
    /**
     * 提交表单
     */
    onSubmit() {
      const that = this, U = that.U, fm = that.fm, ps = U.scopy(fm.ps);

      that.$refs[fm.fname].validate((valid) => {
        if (!valid) {
          return false;
        } else {
          fm.sb.load = true;
          
          if(!ps.ids){
            return U.qerrorFalse('请先选择订单');
          }
          
          // if(that.isSelfCreate){
          //   ps.id = ps.ids;
          //   delete ps.ids;

          //   orderSer.createTeamOrder(ps).then((res) => {
          //     fm.sb.load = false;

          //     if (!res || res.code != 1) {
          //       U.qerror(res.msg);
          //     } else {
          //       U.qsuccess(res.msg);

          //       // 关闭弹框
          //       that.closeMd();

          //       that.$emit("reloadDg");
          //     }
          //   });
          // }else{
            orderSer.createTeamOrder(ps).then((res) => {
              fm.sb.load = false;

              if (!res || res.code != 1) {
                U.qerror(res.msg);
              } else {
                U.qsuccess(res.msg);

                // 关闭弹框
                that.closeMd();

                that.$emit("reloadDg");
              }
            });
          // }
        }
      });
    },
    
  },
};
</script>
<style lang="less" scoped>
.create-team-order{
  .content{
    margin-bottom: 20px;
  }
}
</style>